﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.BLL;
using Jungle.Code.Utils;
using System.Collections.Generic;
using Jungle.Code.DAL;
using Jungle.Code.Core;
using System.Text;
using System.Security.Cryptography;

using Jungle.Users;

	public partial class LoginClass : BaseUserModule
	{
        SessionKeeper thisSession = new SessionKeeper();
        protected override void OnInit(EventArgs e)
        {
            btn_login.PostBackUrl = Request.RawUrl;

            thisSession.SessionId = Session.SessionID.ToString();
            thisSession.GetData();

            if (thisSession.UserId == -1)
            {
                LoginMenu.Visible = true;
                LoginMenu.ActiveViewIndex = 0;

                HttpCookie cookie = Request.Cookies["RM3ffd3sdfsdfsdf9b2a8923b0b48b3c"];
                if (cookie != null)
                {
                    if (cookie["rm"] == "true")
                    {
                        chb_Remember.Checked = true;

                        thisSession.SessionId = Session.SessionID.ToString();
                        thisSession.UserId = Convert.ToInt32(cookie["ui"]);
                        thisSession.UserLogin = cookie["nm"];
                        thisSession.AdminLogin = "none";

                        thisSession.Create();

                        Response.Redirect(UrlHelper.GetSiteUrl());

                    }
                }
            }
            else
            {
                LoginMenu.Visible = true;
                LoginMenu.ActiveViewIndex = 1;
                LoadMenu();
            }

            base.OnInit(e);
        }
        // -----------------------------------------------------------------------------------------
        protected void Logout(object sender, EventArgs e)
        {
            thisSession.DeleteSession(Session.SessionID.ToString());

            HttpCookie cookie = Response.Cookies["RM3ffd3sdfsdfsdf9b2a8923b0b48b3c"];
            cookie["rm"] = "false";

            Response.Redirect(UrlHelper.GetSiteUrl());
        }
        // -----------------------------------------------------------------------------------------
        protected void Login(object sender, EventArgs e)
        {
            UserManager um = new UserManager();
            um.Email = tb_login.Text;
            um.Password = UserManager.getMd5Hash(tb_password.Text.Trim());

            if (um.Login())
            {
                thisSession.SessionId = Session.SessionID.ToString();
                thisSession.UserId = um.UserId;
                thisSession.UserLogin = um.Email;
                thisSession.AdminLogin = "none";

                thisSession.Create();

                if (chb_Remember.Checked)
                {
                    HttpCookie cookie = Response.Cookies["RM3ffd3sdfsdfsdf9b2a8923b0b48b3c"];
                    cookie["rm"] = "true";
                    cookie["nm"] = um.Email;
                    cookie["ui"] = um.UserId.ToString();
                    cookie.Expires = DateTime.Now.AddDays(14);
                }

                Response.Redirect(UrlHelper.GetSiteUrl());
            }
            else
            {
                lb_err.Visible = true;
                lb_err.Text = "Incorrect login or password";
            }
        }
        // -----------------------------------------------------------------------------------------
        private void LoadMenu()
        {
            switch(new UserManager(thisSession.UserId).RoleId)
            {
                case 6:
                    HyperLink hp_offers = new HyperLink();
                    hp_offers.Text = "Добавить объект";
                    hp_offers.NavigateUrl = UrlHelper.GetSiteUrl() + "/ru/offers.aspx?cmd=addH";
                    ph_Menu.Controls.Add(hp_offers);
                    break;
                case 1:
                    HyperLink hp_offers = new HyperLink();
                    hp_offers.Text = "Добавить объект";
                    hp_offers.NavigateUrl = UrlHelper.GetSiteUrl() + "/ru/offers.aspx?cmd=addH";
                    ph_Menu.Controls.Add(hp_offers);
                    break;
            }
        }
	}
